package dk.zpon.andnoise;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import dk.zpon.andnoise.R;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BrowserAdapter extends BaseAdapter {

	private Connection mCon;
	private LayoutInflater mInflater;
	private FilefolderHolder mViews[];
	private JSONObject mData;
	private int mCount;
	private Context mContext;
	private int mScroll;
	private int mFirstVisible;
	private JSONArray mFolders, mFiles;

	public BrowserAdapter(Context context)
	{
		mInflater = LayoutInflater.from(context);
		mCon = Connection.getInstance();
		mData = mCon.browserGetCurrentFilesAndFolders();

		try
		{
			mFolders = mData.getJSONArray("FOLDERS");
			mFiles = mData.getJSONArray("FILES");
			// Up one level
			if(!mCon.browserGetCurrentPath().equals("/"))
			{
				mCount = mFolders.length()+mFiles.length()+1; // folders + files + "back/.."
				mViews = new FilefolderHolder[mCount];

				View view = mInflater.inflate(R.layout.browser_entry, null);
				mViews[0] = new FilefolderHolder("Back", true, true, 0);
				mViews[0].getView(view);
				view.setTag(mViews[0]);
			}
			else
			{
				// Root does not have "go back"
				mCount = mFolders.length()+mFiles.length(); // folders + files
				mViews = new FilefolderHolder[mCount];
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setFirstVisible(int i) {
		// TODO Auto-generated method stub
		this.mFirstVisible = i;
	}

	public int getFirstVisible()
	{
		return this.mFirstVisible;
	}

	public void setScroll(int scroll) {
		this.mScroll = scroll;
	}

	public int getScroll()
	{
		return mScroll;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public FilefolderHolder getHolder(int position)
	{
		if(this.mViews.length > position)
			return this.mViews[position];
		else
			return null;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) 
	{
		View convertView;
		FilefolderHolder ffh;
		
		if(view != null)
			convertView = view;
		else
			convertView = mInflater.inflate(R.layout.browser_entry, null);
		
		if(mViews[position] == null)
		{
			try
			{
				int subtractFile, subtractFolder, folderDevider;

				if(mCon.browserGetCurrentPath().equals("/"))
				{
					folderDevider = mData.getJSONArray("FOLDERS").length()-1;
					subtractFolder = 0;
					subtractFile = mData.getJSONArray("FOLDERS").length();
				}
				else
				{
					folderDevider = mData.getJSONArray("FOLDERS").length();
					subtractFolder = 1;
					subtractFile = mData.getJSONArray("FOLDERS").length()+1;
				}

				
				if(position <= folderDevider)
				{

					ffh = new FilefolderHolder(mData.getJSONArray("FOLDERS").getString(position-subtractFolder), true, false, position);
				}
				else
				{
					ffh = new FilefolderHolder(mData.getJSONArray("FILES").getString(position-subtractFile), false, false, position);
				}
				
				mViews[position] = ffh;
				
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		else
			ffh = mViews[position];

		convertView.setTag(ffh);
		ffh.getView(convertView);
		
		return convertView;
	}

	class FilefolderHolder 
	{
		private String mName;
		private boolean mIsFolder;
		private boolean mIsUpOneLevel;
		private int mPosition;

		public FilefolderHolder(String name, boolean isFolder, boolean isUpOneLevel, int position)
		{
			mName = name;
			mIsFolder = isFolder;
			mIsUpOneLevel = isUpOneLevel;
			mPosition = position;
		}

		public void getView(View view)
		{
			TextView viewName = (TextView) view.findViewById(R.id.browser_name);
			ImageView viewIcon = (ImageView) view.findViewById(R.id.browser_icon);

			viewName.setText(this.mName);

			viewName.setTextColor(Color.rgb(189, 189, 189));
			
			if(this.mIsFolder && this.mIsUpOneLevel)
			{
				viewName.setTextColor(Color.WHITE);
				viewIcon.setImageBitmap(BitmapFactory.decodeResource(view.getResources(), R.drawable.back));
			}
			else if(this.mIsFolder)
				viewIcon.setImageBitmap(BitmapFactory.decodeResource(view.getResources(), R.drawable.folder));
			else
				viewIcon.setImageBitmap(BitmapFactory.decodeResource(view.getResources(), R.drawable.file));
		}

		public int getPosition()
		{
			return this.mPosition;
		}

		public boolean getIsUpOneLevel()
		{
			return this.mIsUpOneLevel;
		}

		public String getName()
		{
			return this.mName;
		}

		public boolean isFolder()
		{
			return this.mIsFolder;
		}
	}
}